package sv.ues.fia.activities;

import sv.ues.fia.R;
import sv.ues.fia.bdcontrol.ControlBDHelper;
import sv.ues.fia.session.UsuarioActual;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
	final String ADMIN = "admin";
	final String TEACHER = "teacher";
	final String ALUMNO = "carnet";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		final Animation animRotate = AnimationUtils.loadAnimation(this,R.anim.animation);		
		Button btnLogin = (Button) findViewById(R.id.btnLogin);
		final EditText user = (EditText)findViewById(R.id.userForm);
		final EditText pass = (EditText)findViewById(R.id.passForm);
		final CheckBox rememberMe = (CheckBox)findViewById(R.id.cbxRememberme);		
		
		SharedPreferences prefs = getSharedPreferences("usuario",
				Context.MODE_PRIVATE);
		
		if(prefs.getBoolean("REMEMBERME", false)){
			UsuarioActual actualUser = UsuarioActual.getInstance();			
			actualUser.setPassUsuario(prefs.getString("USER", ADMIN));
			actualUser.setTipoUsuario(prefs.getInt("TYPE", 1));
			
			Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
			LoginActivity.this.finish();
			LoginActivity.this.startActivity(intent);
		}
		
		btnLogin.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				arg0.startAnimation(animRotate);
				boolean valid=false;
				
				if(user.getText().toString().compareTo(ADMIN)==0 || pass.getText().toString().compareTo(ADMIN)==0){
					savePreference(ADMIN, ADMIN, UsuarioActual.ADMIN, rememberMe.isChecked());
					valid=true;
				}else if(user.getText().toString().compareTo(TEACHER)==0 || pass.getText().toString().compareTo(TEACHER)==0){
					savePreference(TEACHER, TEACHER, UsuarioActual.TEACHER, rememberMe.isChecked());
					valid=true;
				}else if(user.getText().toString().compareTo(ALUMNO)==0 || pass.getText().toString().compareTo(ALUMNO)==0){
					savePreference(ALUMNO, ALUMNO, UsuarioActual.STUDENT, rememberMe.isChecked());
					valid=true;
				}else{
					Toast.makeText(LoginActivity.this, LoginActivity.this.getString(R.string.invalidUser), Toast.LENGTH_LONG).show();					
				}							
				
				if(valid){
					Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
					LoginActivity.this.finish();
					LoginActivity.this.startActivity(intent);
				}
			}
		});			
	}
	
	private void savePreference(String user, String pass, int type, boolean remember) {
		
		
		SharedPreferences prefs = getSharedPreferences("usuario",
				Context.MODE_PRIVATE);
		
		UsuarioActual actualUser = UsuarioActual.getInstance();
		actualUser.setNombreUsuario(user);
		actualUser.setPassUsuario(pass);
		actualUser.setTipoUsuario(type);
		
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString("USER", user);
		editor.putInt("TYPE", type);
		editor.putBoolean("REMEMBERME", remember);
		editor.commit();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.login_options, menu);
		return true;

	}
	
	/** Metodo controlador de acciones del menu general **/
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
	
		switch (item.getItemId()) {		
		case R.id.action_about:
			
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(this.getString(R.string.infoApp))
			       .setCancelable(false)
			       .setNegativeButton("OK", new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {			                			        	   			        	  
			        	   dialog.cancel();
			           }
			       });
			AlertDialog alert = builder.create();
			alert.show();
			
			break;
		case R.id.action_poblateBD:
			
			ControlBDHelper bd = new ControlBDHelper(this);
			bd.abrir();
			// LLenar base de datos con bd.llenarBD y mostrarlo en el Toast
			Toast.makeText(this, bd.llenarBD(), Toast.LENGTH_LONG).show();
			bd.cerrar();
			
			break;
			
		default:
			break;
		}

		return super.onContextItemSelected(item);
	}

}
